﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESF.General.Entity;
using ESF.Web.Pages;
using ESF.SSO.Presenter.Admin.Menu;
using ESF.General;
using ESF.Web;
using ESF.SSO.Core.Entities.BasicReferences;

#endregion


public partial class Menu_EditGeneralMenu : BasePagePresenter<EditGeneralMenuPresenter>, IEditGeneralMenuViewer
{
    #region Carga Inicial
	
	protected void Page_Load(object sender, EventArgs e)
    {
		_Presenter = new EditGeneralMenuPresenter (this);

		if (!IsPostBack)
		{
			lblTitle.Text = Title;

			ListLoader.LoadList (ddlApplication, WebGeneral.ListBasicReference (BasicReferenceTypes.Application));

			_Presenter.Load ();
		}
    }

	#endregion


	#region Campos del Interface

	public int? ApplicationID
	{
		get
		{
			return TextHelper.GetNullableInt (ddlApplication.SelectedValue);
		}
		set
		{
			ListLoader.Select (ddlApplication, value);
		}
	}


	public List<UserMenuItem> lstMainUserMenus
	{
		set
		{
			if (value == null)
			{
				uscGenericTreeView.RemoveAllNodes ();
				divEdition.Visible = false;
			}
			else
			{			
				uscGenericTreeView.SetRootNodes (value.ToArray ()); 
				divEdition.Visible = true;
			}
		}
	}


	public UserMenuItem SelectedUserMenu
	{
		get
		{
			return uscEditUserMenu.UserMenuItem;
		}
		set
		{
			uscEditUserMenu.UserMenuItem = value;
		}
	}

	#endregion


	#region Eventos de los botones

	protected void btnClear_Click(object sender, EventArgs e)
	{
		ddlApplication.Enabled = true;

		_Presenter.Clear ();
	}

	protected void btnShowMenuForApp_Click(object sender, EventArgs e)
	{
		ddlApplication.Enabled = false;
		
		_Presenter.ShowMainUserMenus ();
	}

	protected void btnAddNewRoot_Click(object sender, EventArgs e)
	{
		uscEditUserMenu.UserMenuItem = new UserMenuItem ()
		{
			Application = new BasicReference () { ID = ApplicationID }
		};

		uscEditUserMenu.Visible = true;
	}


	protected void btnAddNewChild_Click(object sender, EventArgs e)
	{
		Dictionary<string, string> nodeData;
		
		
		nodeData = uscGenericTreeView.GetSelectedNodeData ();

		if (nodeData == null)
		{
			JS.AddWarning (Page, "Seleccione primero un elemento de menu como el padre");
			return;
		}


		uscEditUserMenu.UserMenuItem = new UserMenuItem ()
		{
			Application = new BasicReference () { ID = ApplicationID },
			Parent = new UserMenuItem () { ID = TextHelper.GetNullableInt (nodeData ["ID"]), Code = nodeData ["Code"] }
		};

		uscEditUserMenu.Visible = true;
	}

	#endregion	


	#region Eventos del Arbol

	protected object[] uscGenericTreeView_SelectNodeGetChildren(Dictionary<string, string> pValueList, bool pFromExpand, int Depth)
	{
		UserMenuItem tmp;


		uscEditUserMenu.Visible = false;
		
		if (pValueList != null)
		{
			tmp = new UserMenuItem () { ID = TextHelper.GetNullableInt (pValueList ["ID"]) };

			uscEditUserMenu.UserMenuItem = _Presenter.GetUserMenu (tmp);
			
			if (pFromExpand)
			{	
				return _Presenter.GetUserMenusByLevel (tmp.ID).ToArray ();
			}
		}

		return null;
	}
	
	#endregion


	#region Eventos de la Captura de Menu

	protected int? uscEditUserMenu_OnSave (UserMenuItem pUserMenuItem)
	{
		pUserMenuItem.ID = _Presenter.SaveUserMenu (pUserMenuItem);
		
		
		if (pUserMenuItem.Parent == null)
		{
			uscGenericTreeView.AddRootNode (pUserMenuItem);
		}
		else
		{
			if (!uscGenericTreeView.AddChildNode (pUserMenuItem.Parent.ID.ToString (), pUserMenuItem))
			{
				JS.AddWarning (Page, string.Format ("No fue encontrado el nodo padre con identificador {0}\npara agregar el nodo con identificador {1}", pUserMenuItem.Parent.ID, pUserMenuItem.ID));
			}
		}
		
		updEdition.Update ();

		return pUserMenuItem.ID;
	}


	protected void uscEditUserMenu_OnDelete (UserMenuItem pUserMenuItem)
	{
		_Presenter.DeleteUserMenu (pUserMenuItem);

		if (!uscGenericTreeView.DeleteNode (pUserMenuItem.ID.ToString ()))
		{
			JS.AddWarning (Page, string.Format ("No fue encontrado un nodo con identificador {0}", pUserMenuItem.ID));
		}

		updEdition.Update ();
	}

	#endregion	
}